import CONFIG from './config.js'
import GameMap from './game_map.js'
import SpiritUtils from './utils_spirit.js'
import WeaponUtils from './utils_weapon.js'
import GameEvent from './event.js'
import Debug from './debug.js'
import GameInfo from './game_info.js'
import LevelManager from './level_mng.js'
import Message from './message.js'

class Game {
    constructor() {
        let canvas_spirit = document.getElementById(CONFIG.canvas_stage_id);
        let stage_canvas_ctx = canvas_spirit.getContext("2d");

        this.map = new GameMap(this);
        this.spirit_utils = new SpiritUtils(this);
        this.weapon_utils = new WeaponUtils(this);
        this.config = JSON.parse(JSON.stringify(CONFIG));
        this.game_event = new GameEvent(this);

        this.stage_canvas_ctx = stage_canvas_ctx;
        this.debug = Debug && new Debug(this);

        this.game_info = new GameInfo(this);
        this.level_manager = new LevelManager(this);

        this.show_banner();
    }

    show_banner() {
        this.map.draw();
        this.debug && this.debug.show();

        let msg = {text: "欢迎体验", size: 2};
        Message.add_message(msg);
        msg = {text: "塔防游戏", size: 6};
        Message.add_message(msg);
        msg = {text: "适度娱乐，有益身心。", size: 2};
        Message.add_message(msg);
        msg = {text: "过度游戏，伤害健康。", size: 2};
        Message.add_message(msg);

        Message.show_msg(this);

    }

    init() {
        this.game_info = new GameInfo(this);
        this.config = JSON.parse(JSON.stringify(CONFIG));
        this.level_manager = new LevelManager(this);
        Message.clear_message();
        this.map.init_map();
    }

    start() {
        let game = this;
        let map = this.map;

        this.init();

        map.calc_path();
        game.game_info.status = "running";

        this.animation_handler && window.cancelAnimationFrame(this.animation_handler);
        this.animation_handler = window.requestAnimationFrame(function () {
            game.loop();
        });
    }

    check_game_over() {
        if (this.game_info.is_stop) return;

        if (this.game_info.escape_spirit_count >= CONFIG.spirit.max_escape) {
            let msg = {text: "游戏结束", size: 6, type: "warning", duration: 0};
            Message.add_message(msg);
            Message.show_msg(this);
            this.game_info.status = "stop";
            this.game_event.reset_handle();
        }
    }

    loop() {
        let game = this;
        this.game_info.loop_count++;

        if (this.game_info.loop_count % 15 === 0) {
            this.map.draw();
            this.weapon_utils.draw_weapon();
            this.game_event.draw();
        }

        this.debug && this.debug.show();

        let ctx = this.stage_canvas_ctx;
        ctx.lineDashOffset -= 1;
        if (ctx.lineDashOffset < -120) {
            ctx.lineDashOffset = 0;
        }

        this.spirit_utils.clear_canvas();
        this.spirit_utils.draw_spirit();
        this.weapon_utils.draw_bullet();
        if (this.game_info.is_running) {
            if (this.level_manager.is_running) {
                this.spirit_utils.gen_spirit();
            }
            this.spirit_utils.spirit_move();
            this.spirit_utils.cls_spirit();

            this.weapon_utils.weapon_fire();
            this.weapon_utils.bullet_move_on();
            this.weapon_utils.bullet_check_hit();
            this.weapon_utils.cls_bullet();

            this.level_manager.handle_level();
        }
        Message.show_msg(game);

        this.check_game_over();

        this.animation_handler = window.requestAnimationFrame(function () {
            game.loop();
        });
    }
}

export default Game;

